﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Client.InfoSys;

namespace Zwms.Client.OutWms
{
    public partial class NewOutplanBillForm : BaseForm
    {
        #region  构造函数
        public NewOutplanBillForm()
        {
            InitializeComponent();
            InitBtnImage();
            InitInPlanBillCode();
        }

        private void InitInPlanBillCode()
        {
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();

            tbOutPlanBillCode.Text = client.InitOutPlanBillCode();
            dpuPlanOutDate.SetDate(client.GetCurrentSystemTime());
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.exit;
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (cmbOBType.SelectedIndex == -1)
            {
                bRet = false;
                cmbOBType.BackColor = Color.Yellow;
            }
            else if (cmbOBType.SelectedIndex == 1 && tbsReciever.GetTextBoxContent() == "")
            {
                MessageBox.Show("货物的接收仓库！");
                cmbOBType.BackColor = Color.Yellow;
                return;
            }

            if (!bRet)
            {
                return;
            }

            if (MessageBox.Show("是否确认添加该入库计划单？", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                BilOutPlanBill record = new BilOutPlanBill();
                record.Id = tbOutPlanBillCode.Text;
                if (cmbOBType.SelectedIndex != -1)
                {
                    record.OutType = cmbOBType.SelectedIndex.ToString("D1");
                }

                record.SendBillCode = tbSendBillCode.Text;
                record.POCode = tbPOCode.Text;
                record.WarehouseCode = MainForm.CurrentUser.WarehouseCode;
                record.WarehouseName = MainForm.CurrentUser.WarehouseName;
                record.BillStatus = "0";
                
                if (!string.IsNullOrWhiteSpace(tbsSender.GetTextBoxContent()))
                {
                    record.SenderName = tbsSender.GetTextBoxContent();
                    record.SenderCode = tbsSender.Tag.ToString();
                }

                if (!string.IsNullOrWhiteSpace(tbsReciever.GetTextBoxContent()))
                {
                    record.RecieverName = tbsReciever.GetTextBoxContent();
                    record.RecieverCode = tbsReciever.Tag.ToString();
                }


                ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                record.CreateUserName = MainForm.CurrentUser.UserName;
                record.CreateUserCode = MainForm.CurrentUser.Id;
                record.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                record.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                record.EditUserName = "";
                record.EditUserCode = "";
                record.EditDate = "";
                record.EditTime = "";

                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();

                if (client.AddOutPlanBill(record))
                {
                    SysLogWriter.WriteOptLog("手动添加出库计划单", record.Id, "改");
                    MessageBox.Show("成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("未能添加，请检查编号是否已存在！");
                    return;
                }

            }
            else
            {
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tbsSender_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.CompanyWnd uc = new InfoWms.CompanyWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择发货方");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsSender.SetTextBoxContent(uc.SelectedRecord.CompanyName);
                tbsSender.Tag = uc.SelectedRecord.Id;
            }
        }

        private void tbsReciever_SelectButtonClick(object sender, EventArgs e)
        {
            if (cmbOBType.SelectedIndex == 0)
            {
                InfoWms.CompanyWnd uc = new InfoWms.CompanyWnd();
                CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择收货方");
                uc.SetQueryState();
                if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    tbsReciever.SetTextBoxContent(uc.SelectedRecord.CompanyName);
                    tbsReciever.Tag = uc.SelectedRecord.Id;
                }
            }
            else if(cmbOBType.SelectedIndex == 1)
            {
                InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
                CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择收货方");
                uc.SetQueryState();
                if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    tbsReciever.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                    tbsReciever.Tag = uc.SelectedRecord.Id;
                }
            }
        }

        private void cmbOBType_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbsReciever.SetTextBoxContent("");
            tbsReciever.Tag = "";
        }
        #endregion
    }
}
